* February 14, 2021
* For each shaming condition and each of 5 types of action,
*    Weight to match party proportions in the population, and weight
*    to adjust for any imblances in the cost given that type of action
* wt1 is weight if pid3==1 (Dem)
* wt2 is weight if pid3==2 (Ind)
* wt3 = (1 - wt1 - wt) is weight if pid3==3 (Rep)
* wtvar(str) is name of new variable which contains the weights
program define mkwts3
   version 16.1
	syntax, WT1(str) WT2(str) WTVAR(str)
   
	local wt3 = 1-`wt1'-`wt2'
   gen `wtvar' = .	// initialize
	
	* Weights when action condition is 0
	quietly {			
		forvalues s = 0/9 {  // loop over shaming conditions
			forvalues a = 0/0 {  // action condition is 0
				count if shamed==`s' & axn5==`a'
				local N `r(N)'
				forvalues p = 1/3 { // loop over pid		      			
					count if shamed==`s' & axn5==`a' & pid3==`p'		
					replace `wtvar' = `wt`p''*`N'/`r(N)' if shamed==`s' & axn5==`a' & pid3==`p'         
				}
			}	
      }			
	}
	
	* Weights when action condition is 1 or 2
	quietly {			
		forvalues s = 0/9 {  // loop over shaming conditions
			count if shamed==`s' & (axn5==1 | axn5==2)
			local N `r(N)'
			forvalues a = 1/2 {  // loop over action conditions 1 and 2
			   forvalues p = 1/3 { // loop over pid		      			
				   count if shamed==`s' & axn5==`a' & pid3==`p'		
					replace `wtvar' = .5*`wt`p''*`N'/`r(N)' if shamed==`s' & axn5==`a' & pid3==`p'         
				}
			}	
      }			
	}
	
	* Weights when action condition is 3 or 4
	quietly {			
		forvalues s = 0/9 {  // loop over shaming conditions
			count if shamed==`s' & (axn5==3 | axn5==4)
			local N `r(N)'
			forvalues a = 3/4 {  // loop over action conditions 3 and 4
			   forvalues p = 1/3 { // loop over pid		      			
				   count if shamed==`s' & axn5==`a' & pid3==`p'		
					replace `wtvar' = .5*`wt`p''*`N'/`r(N)' if shamed==`s' & axn5==`a' & pid3==`p'         
				}
			}	
      }			
	}
	
	bysort shamed: table axn5 pid3, c(mean `wtvar' n `wtvar')	
	
end
